package com.ningmengs789.live.live.live.common.widget.red;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.ningmengs789.live.R;
import com.ningmengs789.live.http.JsonBuilder;
import com.ningmengs789.live.http.OKHttpUtils;
import com.ningmengs789.live.Interface.RequestCallback;
import com.ningmengs789.live.live.live.common.widget.gift.AbsDialogFragment;
import com.ningmengs789.live.live.live.common.widget.gift.utils.OnItemClickListener;
import com.ningmengs789.live.live.response.BonusReceivedList;
import com.ningmengs789.live.net.utils.json.JsonMananger;
import com.ningmengs789.live.server.widget.CircleImageView;
import com.ningmengs789.live.ui.widget.shimmer.SwipeRefreshHelper;
import com.ningmengs789.live.ui.widget.shimmer.VerticalSwipeRefreshLayout;
import com.ningmengs789.live.utils.CommonUtils;
import com.bumptech.glide.Glide;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

public class HistoryRPDialogFragment extends AbsDialogFragment implements OnItemClickListener {

    private HistoryRPAdapter mAdapter;
    private List<BonusReceivedList.ListBean> mList = new ArrayList<>();

    private VerticalSwipeRefreshLayout mSwipeRefresh;
    private RecyclerView shimmerRecycler;
    private LinearLayoutManager linearLayoutManager;

    private TextView mName;
    private String name;

    private String mBonusId;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_robrp_item_4;
    }

    @Override
    protected int getDialogStyle() {
        return R.style.BottomDialog2;
    }

    @Override
    protected boolean canCancel() {
        return true;
    }

    @Override
    protected void setWindowAttributes(Window window) {
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle == null) {
            return;
        }
        mBonusId = bundle.getString("bonus_id");
        String avatar = bundle.getString("avatar");
        name = bundle.getString("name");

        CircleImageView mAvatar = mRootView.findViewById(R.id.avatar);
        mName = mRootView.findViewById(R.id.name);
        mName.setText(name + "的红包");
        Glide.with(mContext)
                .load(CommonUtils.getUrl(avatar))
                .into(mAvatar);

        mSwipeRefresh = mRootView.findViewById(R.id.swipe_refresh);
        shimmerRecycler = mRootView.findViewById(R.id.shimmer_recycler_view);

        linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        shimmerRecycler.setLayoutManager(linearLayoutManager);
        mAdapter = new HistoryRPAdapter(mContext, this);
        shimmerRecycler.setAdapter(mAdapter);

        initSwipeRefresh();
        loadData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void loadData() {
        String body = "";
        try {
            body = new JsonBuilder()
                    .put("bonus_id", mBonusId)
                    .build();
        } catch (JSONException e) {
        }
        OKHttpUtils.getInstance().getRequest("app/Appbonus/bonusReceivedList", body, new RequestCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
                finishRefresh();
            }
            @Override
            public void onSuccess(String result) {
                finishRefresh();
                try{
                    BonusReceivedList response = JsonMananger.jsonToBean(result, BonusReceivedList.class);
                    mName.setText(name + "的红包" + response.getNum());

                    if (mList.size() > 0) {
                        mList.clear();
                    }

                    mList.addAll(response.getList());
                    mAdapter.setCards(mList);

                    mAdapter.notifyDataSetChanged();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    private void finishRefresh() {
        if (mSwipeRefresh != null) {
            mSwipeRefresh.setRefreshing(false);
        }
    }

    private void initSwipeRefresh() {
        if (mSwipeRefresh != null) {
            SwipeRefreshHelper.init(mSwipeRefresh, new VerticalSwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    loadData();
                }
            });
        }
    }

    @Override
    public void onItemClick(Object bean, int position) {
        //dismiss();
    }
}
